package com.mediacloud.app.newsmodule.fragment.baoliao;

import com.alibaba.fastjson.JSON;
import com.mediacloud.app.user.utils.LogUtil;
import com.mediacloud.app.model.news.SetOriginData;

import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.functions.Function;

/**
 * Created by Yoake on 2018/8/29.
 */

public class TransUtils {
    public static <T> ObservableTransformer<JSONObject, T> fastJSonTransform(final Class<T> classRef) {
        return new ObservableTransformer<JSONObject, T>() {
            @Override
            public ObservableSource<T> apply(Observable<JSONObject> upstream) {
                return upstream.map(new Function<JSONObject, T>() {
                    @Override
                    public T apply(JSONObject s) throws Exception {
                        return (T) JSON.parseObject("" + s, classRef);
                    }
                });
            }
        };
    }

    public static <T extends SetOriginData> ObservableTransformer<JSONObject, T> fastJSonTransformByWriteOriginData(final Class<T> classRef) {
        return new ObservableTransformer<JSONObject, T>() {
            @Override
            public ObservableSource<T> apply(Observable<JSONObject> upstream) {
                return upstream.map(new Function<JSONObject, T>() {
                    @Override
                    public T apply(JSONObject s) throws Exception {
                        T result=JSON.parseObject("" + s, classRef);
                        result.setOriginData(s);
                        return result;
                    }
                });
            }
        };
    }
}